package siga.visao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import siga.banco.conexao;
import siga.criptografia.md5;
import siga.pacotao.alertasSistema;
import siga.visao_busca.buscaUsuario;

public class CadastroUsuarios extends javax.swing.JInternalFrame {
    List end_cidade = new ArrayList();
    List end_log_tipo = new ArrayList();
    
    /** Creates new form CadastroPessoas */
    public CadastroUsuarios() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grespfin = new javax.swing.ButtonGroup();
        jPanel7 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        barraFerramentaFonecedor = new javax.swing.JToolBar();
        btNovo = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btBuscar = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        jtfDescricao = new javax.swing.JTextField();
        jtfCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfLogin = new javax.swing.JTextField();
        jtfSenha = new javax.swing.JPasswordField();
        jtfRepSenha = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jtfSituacao = new javax.swing.JComboBox();

        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Cadastro de Pessoa");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/Business_People_16x16.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel7.setEnabled(false);
        jPanel7.setName("jPanel7"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(CadastroUsuarios.class);
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setText("Nome:");
        jLabel1.setName("jLabel1"); // NOI18N

        barraFerramentaFonecedor.setFloatable(false);
        barraFerramentaFonecedor.setRollover(true);
        barraFerramentaFonecedor.setName("barraFerramentaFonecedor"); // NOI18N

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/novo_32x32.png"))); // NOI18N
        btNovo.setMnemonic('N');
        btNovo.setText("Novo");
        btNovo.setToolTipText("Inseri um Novo Cadastro");
        btNovo.setFocusable(false);
        btNovo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btNovo.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btNovo.setName("btNovo"); // NOI18N
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btNovo);

        btAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/editar_32x32.png"))); // NOI18N
        btAlterar.setMnemonic('A');
        btAlterar.setText("Alterar");
        btAlterar.setToolTipText("Alterar um  Cadastro");
        btAlterar.setEnabled(false);
        btAlterar.setFocusable(false);
        btAlterar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btAlterar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btAlterar.setName("btAlterar"); // NOI18N
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btAlterar);

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/cancelar_32x32.png"))); // NOI18N
        btCancelar.setMnemonic('C');
        btCancelar.setText("Cancelar");
        btCancelar.setToolTipText("Cancelar um Cadastro");
        btCancelar.setEnabled(false);
        btCancelar.setFocusable(false);
        btCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btCancelar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btCancelar.setName("btCancelar"); // NOI18N
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btCancelar);

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/salvar_32x32.png"))); // NOI18N
        btSalvar.setMnemonic('S');
        btSalvar.setText("Salvar");
        btSalvar.setToolTipText("Salvar um Cadastro");
        btSalvar.setEnabled(false);
        btSalvar.setFocusable(false);
        btSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btSalvar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btSalvar.setName("btSalvar"); // NOI18N
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btSalvar);

        btBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        btBuscar.setText("Buscar");
        btBuscar.setToolTipText("Buscar um Cadastro");
        btBuscar.setFocusable(false);
        btBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btBuscar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btBuscar.setName("btBuscar"); // NOI18N
        btBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBuscarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btBuscar);

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/sair_32x32.png"))); // NOI18N
        btSair.setMnemonic('S');
        btSair.setText("Sair");
        btSair.setToolTipText("Sair da Janela");
        btSair.setFocusable(false);
        btSair.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btSair.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btSair.setName("btSair"); // NOI18N
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btSair);

        jtfDescricao.setEnabled(false);
        jtfDescricao.setName("jtfDescricao"); // NOI18N

        jtfCodigo.setEditable(false);
        jtfCodigo.setName("jtfCodigo"); // NOI18N

        jLabel2.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel2.setText("Login:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel3.setText("Senha:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setForeground(new java.awt.Color(213, 5, 5));
        jLabel4.setText("Repita Senha:");
        jLabel4.setName("jLabel4"); // NOI18N

        jtfLogin.setEnabled(false);
        jtfLogin.setName("jtfLogin"); // NOI18N

        jtfSenha.setEnabled(false);
        jtfSenha.setName("jtfSenha"); // NOI18N

        jtfRepSenha.setEnabled(false);
        jtfRepSenha.setName("jtfRepSenha"); // NOI18N

        jLabel5.setForeground(new java.awt.Color(213, 5, 5));
        jLabel5.setText("Situação:");
        jLabel5.setName("jLabel5"); // NOI18N

        jtfSituacao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ativado", "Desativado" }));
        jtfSituacao.setEnabled(false);
        jtfSituacao.setName("jtfSituacao"); // NOI18N

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1))
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(636, Short.MAX_VALUE))
            .addComponent(barraFerramentaFonecedor, javax.swing.GroupLayout.DEFAULT_SIZE, 690, Short.MAX_VALUE)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jtfLogin, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jtfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfRepSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))))
                .addContainerGap(318, Short.MAX_VALUE))
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(615, Short.MAX_VALUE))
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jtfSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(538, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(barraFerramentaFonecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(4, 4, 4)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfRepSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        btSalvar.setEnabled(!btSalvar.isEnabled());
        btCancelar.setEnabled(!btCancelar.isEnabled());
        btNovo.setEnabled(!btNovo.isEnabled());
        btAlterar.setEnabled(false);
        btBuscar.setEnabled(!btBuscar.isEnabled());
        gerenciaCampos();
        LimpaCampos();
        conexao.AutoIncrementa("usuarios");
        jtfCodigo.setText(conexao.AutoIncRS);
        this.setTitle("Usuário :: Código:" + conexao.AutoIncRS);
        
}//GEN-LAST:event_btNovoActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        btBuscar.setEnabled(!btBuscar.isEnabled());
        gerenciaBotao();
        gerenciaCampos();
}//GEN-LAST:event_btAlterarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        if(btCancelar.isEnabled()){
            if(!btNovo.isEnabled()){
                btSalvar.setEnabled(!btSalvar.isEnabled());
                btCancelar.setEnabled(!btCancelar.isEnabled());
                btNovo.setEnabled(!btNovo.isEnabled());
                btBuscar.setEnabled(!btBuscar.isEnabled());
            }else{
                gerenciaBotao();
            }
            conexao.RemoveAutoIncrementa("usuarios");
            this.setTitle("Usuários");
            gerenciaCampos();
            LimpaCampos();
        }
}//GEN-LAST:event_btCancelarActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed

        if(jtfSenha.getText().equals(jtfRepSenha.getText())){
            if(!jtfDescricao.getText().isEmpty()){
                String dados = "";
                String campos = "";
                dados = jtfCodigo.getText();
                campos = "codigo";
                dados += "," + index.cod_emp;
                campos += ",cod_emp";
                dados += (!jtfDescricao.getText().isEmpty()) ? ",\"" + jtfDescricao.getText() + "\"" : "";
                campos += (!jtfDescricao.getText().isEmpty()) ? ",descricao" : "";
                
                dados += (!jtfLogin.getText().isEmpty()) ? ",\"" + jtfLogin.getText() + "\"" : "";
                campos += (!jtfLogin.getText().isEmpty()) ? ",login" : "";
                
                dados += (!jtfSenha.getPassword().toString().isEmpty()) ? ",\"" + md5.crpMD5(jtfSenha.getText()) + "\"" : "";
                campos += (!jtfSenha.getPassword().toString().isEmpty()) ? ",senha" : "";
                
                dados += (!jtfSituacao.getSelectedItem().toString().isEmpty()) ? ",\"" + jtfSituacao.getSelectedItem().toString().substring(0, 1) + "\"" : "";
                campos += (!jtfSituacao.getSelectedItem().toString().isEmpty()) ? ", modulo" : "";
                
                conexao.Inserir("replace into usuarios (" + campos + ") values(" + dados + ")");
                gerenciaBotao();
                btBuscar.setEnabled(!btBuscar.isEnabled());
                gerenciaCampos();
           }else{
               alertasSistema.alertaPermissao();
           }
        }else{
            alertasSistema.alertaErro("Senhas não conferem.");
        }
}//GEN-LAST:event_btSalvarActionPerformed

    @SuppressWarnings("static-access")
    private void btBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBuscarActionPerformed
        buscaUsuario bt = new buscaUsuario(null,true);
        bt.setLocation((index.tela.width-bt.getWidth())/2, (index.tela.height-bt.getHeight())/2);
        bt.setVisible(true);
        if(bt.isConfirmaCurso()){
            gerenciaBusca(index.LRcurso.get(0).toString());
            index.LRcurso.clear();
            btAlterar.setEnabled(true);
        }
}//GEN-LAST:event_btBuscarActionPerformed

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        btCancelarActionPerformed(evt);
        setVisible(false);
        dispose();
}//GEN-LAST:event_btSairActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
       
    }//GEN-LAST:event_formInternalFrameOpened

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
        if(btCancelar.isEnabled()){
            if(!btNovo.isEnabled()){
                btSalvar.setEnabled(!btSalvar.isEnabled());
                btCancelar.setEnabled(!btCancelar.isEnabled());
                btNovo.setEnabled(!btNovo.isEnabled());
            }else{
                gerenciaBotao();
            }
            conexao.RemoveAutoIncrementa("usuarios");
            this.setTitle("Usuários");
            gerenciaCampos();
            LimpaCampos();
        }
    }//GEN-LAST:event_formInternalFrameClosed

     private void gerenciaBotao(){
        btNovo.setEnabled(!btNovo.isEnabled());
        btAlterar.setEnabled(!btAlterar.isEnabled());
        btCancelar.setEnabled(!btCancelar.isEnabled());
        btSalvar.setEnabled(!btSalvar.isEnabled());
    }

    private void gerenciaCampos(){
        jtfLogin.setEnabled(!jtfDescricao.isEnabled());
        jtfSenha.setEnabled(!jtfDescricao.isEnabled());
        jtfRepSenha.setEnabled(!jtfDescricao.isEnabled());
        jtfSituacao.setEnabled(!jtfDescricao.isEnabled());
        jtfDescricao.setEnabled(!jtfDescricao.isEnabled());
        jtfDescricao.requestFocus();
    }

    private void LimpaCampos(){
        jtfCodigo.setText("");
        jtfDescricao.setText("");
        jtfLogin.setText("");
        jtfSenha.setText("");
        jtfRepSenha.setText("");
    }

    private void gerenciaBusca(String codigo){
        try {
            conexao.Query("select * from usuarios where codigo=" + codigo + " and cod_emp=" + index.cod_emp+" ");
            conexao.res().next();
            jtfCodigo.setText(conexao.res().getString("codigo"));
            jtfDescricao.setText(conexao.res().getString("descricao"));
            jtfLogin.setText(conexao.res().getString("login"));

        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    
  

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar barraFerramentaFonecedor;
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btBuscar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btSair;
    private javax.swing.JButton btSalvar;
    private javax.swing.ButtonGroup grespfin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JTextField jtfCodigo;
    private javax.swing.JTextField jtfDescricao;
    private javax.swing.JTextField jtfLogin;
    private javax.swing.JPasswordField jtfRepSenha;
    private javax.swing.JPasswordField jtfSenha;
    private javax.swing.JComboBox jtfSituacao;
    // End of variables declaration//GEN-END:variables

}
